<?php

namespace Home\Controller;
use Think\Controller;


class LoginController extends Controller {
		
	function login(){
		if(IS_POST){
			$post = getPost();
			$map['username'] = $post['username'];
			$map['password'] =  md5($post['password']);

			$exit = M('user')->where($map)->find();
			if($exit){
				
				session('account',$map['username']);
				
				$e['code'] = 0;
				$e['msg'] = "登录成功";
				$from_url = cookie('from_url');
				
				if($from_url){
					$e['url'] = $from_url;
					cookie('from_url',null);
				}else{
					$e['url'] = U('Index/index');
				}
				
				postReturn($e);
				
			}else{
				$e['code'] = 1;
				$e['msg'] = "用户名或密码错误";
				postReturn($e);
			}
			
		}else{
			
			$this->display();
		}	
	}
	
	function reg(){
		if(IS_POST){
			$post = getPost();	
			$data['username'] = $post['username'];
			$data['password'] = md5($post['password']);
			$data['repassword'] = md5($post['repassword']);
			
			if($data['password']  != $data['repassword'] ){
				$e['code'] = 1;
				$e['msg'] = '两次密码输入不一致';
				postReturn($e);
				return;
			}
			$res = D('user')->reg($data);
			
			postReturn($e);
			
		}else{
			$this->display();
		}	
	}
}